package repository

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/vo"
	"boy-go/pkg/db"
	"boy-go/pkg/request"
	"fmt"
	"gorm.io/gorm"
	"time"
)

type SysPostRepository struct {
	DB *gorm.DB
}

func NewSysPostRepository() *SysPostRepository {
	return &SysPostRepository{
		DB: db.GetMasterDB(),
	}
}

func (r *SysPostRepository) GetById(postId int64) (*model.SysPost, error) {
	db := r.DB
	m := &model.SysPost{}
	err := db.Model(&model.SysPost{}).Where("post_id = ?", postId).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}
func (r *SysPostRepository) SelectPostListByUserId(userId int64) ([]*int64, error) {
	sql := "select p.post_id from sys_post p left join sys_user_post up on up.post_id = p.post_id left join sys_user u on u.user_id = up.user_id  where u.user_id = " + fmt.Sprintf("%d", userId)
	ids := make([]*int64, 0)
	err := r.DB.Raw(sql).Find(&ids).Error
	return ids, err
}
func (r *SysPostRepository) List(query *vo.QuerySysPostReq, pageReq *request.PageRequest) ([]*model.SysPost, int64, error) {
	tx := r.DB.Model(&model.SysPost{})
	if query != nil {
		if query.PostCode != nil {
			tx = tx.Where("post_code like ?", "%"+*query.PostCode+"%")
		}
		if query.PostName != nil {
			tx = tx.Where("post_name = like", "%"+*query.PostName+"%")
		}
		if query.PostSort != nil {
			tx = tx.Where("post_sort = ?", *query.PostSort)
		}
		if query.Status != nil {
			tx = tx.Where("status = ?", *query.Status)
		}
		if query.CreateBy != nil {
			tx = tx.Where("create_by = ?", *query.CreateBy)
		}
		if query.CreateTime != nil {
			tx = tx.Where("create_time = ?", *query.CreateTime)
		}
		if query.UpdateBy != nil {
			tx = tx.Where("update_by = ?", *query.UpdateBy)
		}
		if query.UpdateTime != nil {
			tx = tx.Where("update_time = ?", *query.UpdateTime)
		}
		if query.Remark != nil {
			tx = tx.Where("remark = ?", *query.Remark)
		}
	}
	list := make([]*model.SysPost, 0)
	if pageReq != nil {
		offSet := 0
		limit := 10
		if query != nil {
			if pageReq.PageNum > 0 {
				offSet = (pageReq.PageNum - 1) * limit
			}
			if pageReq.PageSize > 0 {
				limit = pageReq.PageSize
			}
		}
		var total int64
		if err := tx.Count(&total).Error; err != nil {
			return nil, 0, err
		}
		if total > 0 {
			if err := tx.Select("*").Offset(offSet).Limit(limit).Find(&list).Error; err != nil {
				return nil, total, err
			}
		}
		return list, total, nil
	} else {
		if err := tx.Select("*").Find(&list).Error; err != nil {
			return nil, 0, err
		}
	}
	return list, int64(len(list)), nil
}

// Save 添加
func (r *SysPostRepository) Save(m *model.SysPost) error {
	currentTime := time.Now()
	m.CreateAt = &currentTime
	return r.DB.Model(&model.SysPost{}).Create(m).Error
}

// Update 更新
func (r *SysPostRepository) Update(m *model.SysPost) error {
	currentTime := time.Now()
	m.UpdateAt = &currentTime
	return r.DB.Model(&model.SysPost{}).Where("post_id = ?", m.PostId).Updates(m).Error
}

// Delete 删除
func (r *SysPostRepository) Delete(postId int64) error {
	return r.DB.Model(&model.SysPost{}).Where("post_id = ?", postId).Delete(&model.SysPost{}).Error
}

// DeleteBatch 批量删除
func (r *SysPostRepository) BatchDelete(ids []string) error {
	db := r.DB
	db = db.Where("(post_id) IN ?", ids)
	return db.Delete(&model.SysPost{}).Error
}
